package com.liuyi.util;


import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.liuyi.dao.UserDao;
import com.liuyi.service.UserService;

@Component
public class GetLoginInstitution {

	/**
	 * 获取当前登入人的机构id(service中使用，一般为列表)
	 * @param userDao 
	 * @Title: getInstitution 
	 * @Description: TODO
	 * @return
	 * @return: String
	 */
	public static String getInstitution(UserDao userDao){
		JSONObject userInfo = (JSONObject) SecurityUtils.getSubject().getSession().getAttribute("userInfo");
        Integer userId = Integer.valueOf(userInfo.getString("userId"));
        if (userId != null || !"".equals(userId)) {
        	return userDao.selectInstitutionById(userId);
        }else {
        	return null;
        }
	}
	
	/**
	 * 获取当前登入人的姓名
	 * @Title: getUsername 
	 * @Description: TODO
	 * @return
	 * @return: String
	 */
	public static String getUsername(){
		JSONObject userInfo = (JSONObject) SecurityUtils.getSubject().getSession().getAttribute("userInfo");
		String userName = userInfo.getString("username");
		if (userName != null || !"".equals(userName)) {
			return userName;
		}else {
			return null;
		}
	}
	
	/**
	 * 获取当前登入人的id
	 * @Title: getUserId 
	 * @Description: TODO
	 * @return
	 * @return: String
	 */
	public static String getUserId(){
		JSONObject userInfo = (JSONObject) SecurityUtils.getSubject().getSession().getAttribute("userInfo");
		String userId = userInfo.getString("userId");
		if (userId != null || !"".equals(userId)) {
			return userId;
		}else {
			return null;
		}
	}
	
	/**
	 * 获取当前登入的身份
	 * @Title: getIdentity 
	 * @Description: TODO
	 * @return
	 * @return: String
	 */
	public static String getIdentity(){
		JSONObject userInfo = (JSONObject) SecurityUtils.getSubject().getSession().getAttribute("userInfo");
		String identity = userInfo.getString("identity");
		if (identity != null || !"".equals(identity)) {
			return identity;
		}else {
			return null;
		}
	}

	/**
	 * 获取当前登入人的机构id(Controller中使用，一般为新增操作)
	 * @param userDao 
	 * @Title: getInstitution 
	 * @Description: TODO
	 * @return
	 * @return: String
	 */
	public static String getInstitution(UserService userService) {
		JSONObject userInfo = (JSONObject) SecurityUtils.getSubject().getSession().getAttribute("userInfo");
        Integer userId = Integer.valueOf(userInfo.getString("userId"));
        if (userId != null || !"".equals(userId)) {
        	return userService.selectInstitutionById(userId);
        }else {
        	return null;
        }
	}
	
}
